﻿using System;
using UnityEngine;

namespace UnityStandardAssets.ImageEffects
{
	// Token: 0x020005D8 RID: 1496
	[ExecuteInEditMode]
	[AddComponentMenu("Image Effects/Color Adjustments/Contrast Stretch")]
	public class ContrastStretch : MonoBehaviour
	{
		// Token: 0x1700022C RID: 556
		// (get) Token: 0x06001D7F RID: 7551 RVA: 0x00012958 File Offset: 0x00010B58
		public Material materialLum
		{
			get
			{
				if (this.m_materialLum == null)
				{
					this.m_materialLum = new Material(this.shaderLum);
					this.m_materialLum.hideFlags = HideFlags.HideAndDontSave;
				}
				return this.m_materialLum;
			}
		}

		// Token: 0x1700022D RID: 557
		// (get) Token: 0x06001D80 RID: 7552 RVA: 0x0001298F File Offset: 0x00010B8F
		public Material materialReduce
		{
			get
			{
				if (this.m_materialReduce == null)
				{
					this.m_materialReduce = new Material(this.shaderReduce);
					this.m_materialReduce.hideFlags = HideFlags.HideAndDontSave;
				}
				return this.m_materialReduce;
			}
		}

		// Token: 0x1700022E RID: 558
		// (get) Token: 0x06001D81 RID: 7553 RVA: 0x000129C6 File Offset: 0x00010BC6
		public Material materialAdapt
		{
			get
			{
				if (this.m_materialAdapt == null)
				{
					this.m_materialAdapt = new Material(this.shaderAdapt);
					this.m_materialAdapt.hideFlags = HideFlags.HideAndDontSave;
				}
				return this.m_materialAdapt;
			}
		}

		// Token: 0x1700022F RID: 559
		// (get) Token: 0x06001D82 RID: 7554 RVA: 0x000129FD File Offset: 0x00010BFD
		public Material materialApply
		{
			get
			{
				if (this.m_materialApply == null)
				{
					this.m_materialApply = new Material(this.shaderApply);
					this.m_materialApply.hideFlags = HideFlags.HideAndDontSave;
				}
				return this.m_materialApply;
			}
		}

		// Token: 0x06001D83 RID: 7555 RVA: 0x00068A5C File Offset: 0x00066C5C
		public void Start()
		{
			if (!SystemInfo.supportsImageEffects)
			{
				base.enabled = false;
				return;
			}
			if (!this.shaderAdapt.isSupported || !this.shaderApply.isSupported || !this.shaderLum.isSupported || !this.shaderReduce.isSupported)
			{
				base.enabled = false;
				return;
			}
		}

		// Token: 0x06001D84 RID: 7556 RVA: 0x00068AC4 File Offset: 0x00066CC4
		public void OnEnable()
		{
			for (int i = 0; i < 2; i++)
			{
				if (!this.adaptRenderTex[i])
				{
					this.adaptRenderTex[i] = new RenderTexture(1, 1, 0);
					this.adaptRenderTex[i].hideFlags = HideFlags.HideAndDontSave;
				}
			}
		}

		// Token: 0x06001D85 RID: 7557 RVA: 0x00068B14 File Offset: 0x00066D14
		public void OnDisable()
		{
			for (int i = 0; i < 2; i++)
			{
				UnityEngine.Object.DestroyImmediate(this.adaptRenderTex[i]);
				this.adaptRenderTex[i] = null;
			}
			if (this.m_materialLum)
			{
				UnityEngine.Object.DestroyImmediate(this.m_materialLum);
			}
			if (this.m_materialReduce)
			{
				UnityEngine.Object.DestroyImmediate(this.m_materialReduce);
			}
			if (this.m_materialAdapt)
			{
				UnityEngine.Object.DestroyImmediate(this.m_materialAdapt);
			}
			if (this.m_materialApply)
			{
				UnityEngine.Object.DestroyImmediate(this.m_materialApply);
			}
		}

		// Token: 0x06001D86 RID: 7558 RVA: 0x00068BB8 File Offset: 0x00066DB8
		public void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			RenderTexture renderTexture = RenderTexture.GetTemporary(source.width / 1, source.height / 1);
			Graphics.Blit(source, renderTexture, this.materialLum);
			while (renderTexture.width > 1 || renderTexture.height > 1)
			{
				int num = renderTexture.width / 2;
				if (num < 1)
				{
					num = 1;
				}
				int num2 = renderTexture.height / 2;
				if (num2 < 1)
				{
					num2 = 1;
				}
				RenderTexture temporary = RenderTexture.GetTemporary(num, num2);
				Graphics.Blit(renderTexture, temporary, this.materialReduce);
				RenderTexture.ReleaseTemporary(renderTexture);
				renderTexture = temporary;
			}
			this.CalculateAdaptation(renderTexture);
			this.materialApply.SetTexture("_AdaptTex", this.adaptRenderTex[this.curAdaptIndex]);
			Graphics.Blit(source, destination, this.materialApply);
			RenderTexture.ReleaseTemporary(renderTexture);
		}

		// Token: 0x06001D87 RID: 7559 RVA: 0x00068C88 File Offset: 0x00066E88
		public void CalculateAdaptation(Texture curTexture)
		{
			int num = this.curAdaptIndex;
			this.curAdaptIndex = (this.curAdaptIndex + 1) % 2;
			float num2 = 1f - Mathf.Pow(1f - this.adaptationSpeed, 30f * Time.deltaTime);
			num2 = Mathf.Clamp(num2, 0.01f, 1f);
			this.materialAdapt.SetTexture("_CurTex", curTexture);
			this.materialAdapt.SetVector("_AdaptParams", new Vector4(num2, this.limitMinimum, this.limitMaximum, 0f));
			Graphics.SetRenderTarget(this.adaptRenderTex[this.curAdaptIndex]);
			GL.Clear(false, true, Color.black);
			Graphics.Blit(this.adaptRenderTex[num], this.adaptRenderTex[this.curAdaptIndex], this.materialAdapt);
		}

		// Token: 0x0400170A RID: 5898
		[Range(0.0001f, 1f)]
		public float adaptationSpeed = 0.02f;

		// Token: 0x0400170B RID: 5899
		[Range(0f, 1f)]
		public float limitMinimum = 0.2f;

		// Token: 0x0400170C RID: 5900
		[Range(0f, 1f)]
		public float limitMaximum = 0.6f;

		// Token: 0x0400170D RID: 5901
		public RenderTexture[] adaptRenderTex = new RenderTexture[2];

		// Token: 0x0400170E RID: 5902
		public int curAdaptIndex;

		// Token: 0x0400170F RID: 5903
		public Shader shaderLum;

		// Token: 0x04001710 RID: 5904
		public Material m_materialLum;

		// Token: 0x04001711 RID: 5905
		public Shader shaderReduce;

		// Token: 0x04001712 RID: 5906
		public Material m_materialReduce;

		// Token: 0x04001713 RID: 5907
		public Shader shaderAdapt;

		// Token: 0x04001714 RID: 5908
		public Material m_materialAdapt;

		// Token: 0x04001715 RID: 5909
		public Shader shaderApply;

		// Token: 0x04001716 RID: 5910
		public Material m_materialApply;
	}
}
